// EnterPassword.cpp : implementation file
//

#include "stdafx.h"
#include "EnterPassword.h"
#include "CUI.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CEnterPassword dialog

#define ID_SEND_ACK  2

CEnterPassword::CEnterPassword(CWnd* pParent /*=NULL*/)
	: CDialog(CEnterPassword::IDD, pParent)
{

#ifdef _WIN32_WCE
		m_bFullScreen = false;
#endif
	//{{AFX_DATA_INIT(CEnterPassword)
	m_Password = _T("");
	//}}AFX_DATA_INIT
}


void CEnterPassword::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CEnterPassword)
	DDX_Text(pDX, IDC_ConnectPassword, m_Password);
	DDV_MaxChars(pDX, m_Password, 4);
	//}}AFX_DATA_MAP
}


BEGIN_MESSAGE_MAP(CEnterPassword, CDialog)
	//{{AFX_MSG_MAP(CEnterPassword)
	ON_EN_CHANGE(IDC_ConnectPassword, OnChangeConnectPassword)
	ON_WM_TIMER()
	ON_BN_CLICKED(ID_EnterPasswdOK, OnEnterPasswdOK)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CEnterPassword message handlers

void CEnterPassword::OnChangeConnectPassword() 
{
	// TODO: If this is a RICHEDIT control, the control will not
	// send this notification unless you override the CDialog::OnInitDialog()
	// function and call CRichEditCtrl().SetEventMask()
	// with the ENM_CHANGE flag ORed into the mask.
	
	// TODO: Add your control notification handler code here
	CString szPasswd;
	TCHAR   cChar;

	CEdit* pWnd = (CEdit*) GetDlgItem(IDC_ConnectPassword);
	pWnd->GetWindowText(szPasswd);
	if (pWnd != NULL)
	{
		if (szPasswd.GetLength() > 0)
		{
			cChar = szPasswd.GetAt(szPasswd.GetLength()-1);
			if (cChar < '0' || cChar > '9')
			{
				pWnd->SetSel(szPasswd.GetLength()-1,szPasswd.GetLength());
				pWnd->Clear();
			}
			
		}
	}
		
}

BOOL CEnterPassword::OnInitDialog() 
{
	CDialog::OnInitDialog();
	SetTimer(ID_SEND_ACK, 2000, NULL);	
	return TRUE;  // return TRUE unless you set the focus to a control
	              // EXCEPTION: OCX Property Pages should return FALSE
}

int CEnterPassword::DoModal() 
{
	m_Password = _T("");
	return CDialog::DoModal();
}

void CEnterPassword::OnTimer(UINT nIDEvent) 
{
	// TODO: Add your message handler code here and/or call default
	if (g_GlobalObj.m_CMDMsg.SendACK() == ERR_SUCCESS)
	{
		TRACE(_T("ACK success\n"));
	}
	else
	{
		TRACE(_T("ACK error\n"));
	}	
	CDialog::OnTimer(nIDEvent);
}

BOOL CEnterPassword::DestroyWindow() 
{
	// TODO: Add your specialized code here and/or call the base class
	KillTimer(ID_SEND_ACK);	
	return CDialog::DestroyWindow();
}

void CEnterPassword::OnEnterPasswdOK() 
{
	CWnd * pWnd = GetDlgItem(IDC_ConnectPassword);
	pWnd->GetWindowText(g_GlobalObj.m_szPassword);
	CDialog::OnOK();	
}
